package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.${entityName};
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  ${now}
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class ${daoName} extends BaseDao<${entityName}> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<${entityName}> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<${entityName}> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<${entityName}> list = run.query(sql, new ResultSetHandler<List<${entityName}>>() {
            @Override
            public List<${entityName}> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    ${entityName} ${entityName?uncap_first} = new ${entityName}();

                    <#list columnList as column>
                    ${entityName?uncap_first}.set${column.javaName?cap_first}(rs.get${column.resultType}("${column.labelName}"));
                    </#list>

                    list.add(${entityName?uncap_first});
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  ${entityName} 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public ${entityName} selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select ${columnNames} from ${tableName} where id = ?";
        ${entityName} ${entityName?uncap_first} = run.query(sql,new ResultSetHandler<${entityName}>() {
            @Override
            public ${entityName} handle(ResultSet rs) throws SQLException {
                ${entityName} ${entityName?uncap_first} = null;
                if(rs.next()) {
                    ${entityName?uncap_first} = new ${entityName}();

                    <#list columnList as column>
                        ${entityName?uncap_first}.set${column.javaName?cap_first}(rs.get${column.resultType}("${column.labelName}"));
                    </#list>
                }
                return  ${entityName?uncap_first};
            }
        },params);
        return ${entityName?uncap_first};
    }

    /**
    *
    * @param ${entityName?uncap_first} 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(${entityName} ${entityName?uncap_first}) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO ${tableName} (  ");
        sql.append("  ${unPkcolumnNames} ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("    <#list columnList as column> <#if column.labelName != pkColumn.labelName>?<#if (column_index+1) != columnList?size>,</#if></#if> </#list> ");
        sql.append("   )  ");

        Object[] params = {
        <#list columnList as column>
            <#if column.labelName != pkColumn.labelName>${entityName?uncap_first}.get${column.javaName?cap_first}()<#if (column_index+1) != columnList?size>,  </#if></#if>
        </#list>
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  ${entityName?uncap_first}
    * @return
    * @throws SQLException
    */
    @Override
    public int update(${entityName} ${entityName?uncap_first}) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   ${tableName}           ");
        sql.append(" SET                    ");
        <#list columnList as column>
            <#if column.labelName != pkColumn.labelName>sql.append("   ${column.labelName}  = ?<#if (column_index+1) != columnList?size>,  </#if>");</#if>
        </#list>

        sql.append(" WHERE ${pkColumn.labelName}= ?            ");

        Object[] params = {
            <#list columnList as column>
                <#if column.labelName != pkColumn.labelName>${entityName?uncap_first}.get${column.javaName?cap_first}(), </#if>
            </#list>
            ${entityName?uncap_first}.get${pkColumn.javaName?cap_first}()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from ${tableName} where ${pkColumn.labelName} = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
